package com.atguigu.service_edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.service_edu.entity.EduSubject;
import com.atguigu.service_edu.entity.excel.SubjectData;
import com.atguigu.service_edu.entity.subject.OneSubject;
import com.atguigu.service_edu.entity.subject.TwoSubject;
import com.atguigu.service_edu.listener.SubjectExcelListener;
import com.atguigu.service_edu.mapper.EduSubjectMapper;
import com.atguigu.service_edu.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.security.auth.Subject;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author clark
 * @since 2020-04-09
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try{
            InputStream inputStream = file.getInputStream();

            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1.查询出所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //2.查询出所有二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建list集合,用于存储最终封装的数据
        List<OneSubject> finalSubjectList = new ArrayList<>();
        //3.封装一级分类
        for (int i = 0; i < oneSubjectList.size(); i++) {
            OneSubject oneSubject = new OneSubject();
            EduSubject eduSubject = oneSubjectList.get(i);
//            oneSubject.setId(oneSubjectList.get(i).getId());
//            oneSubject.setTitle(oneSubjectList.get(i).getTitle());
            //下面这个是官方提供的工具类,将第一个对象的属性复制到第二个对象的属性当中去
            //效果同等于上面的两个
            BeanUtils.copyProperties(eduSubject,oneSubject);

            for (int j = 0; j < twoSubjectList.size(); j++) {
                EduSubject eduSubject2 = twoSubjectList.get(j);
                if(eduSubject.getId().equals(eduSubject2.getParentId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubject2,twoSubject);
                    oneSubject.getChildren().add(twoSubject);
                    twoSubjectList.remove(eduSubject2);
                    j--;
                }
            }
            finalSubjectList.add(oneSubject);
        }
        return finalSubjectList;
    }




}
